<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="类别(内控、统计月)" prop="type1">
        <el-input
          v-model="queryParams.type1"
          placeholder="请输入类别(内控、统计月)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="gg">
        <el-input
          v-model="queryParams.gg"
          placeholder="请输入产品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="ggname">
        <el-input
          v-model="queryParams.ggname"
          placeholder="请输入产品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="counts">
        <el-input
          v-model="queryParams.counts"
          placeholder="请输入数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="统计时间" prop="tjsj">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.tjsj"
          type="month"
          value-format="yyyy-MM"
          placeholder="选择统计时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属工厂" prop="factoryId" v-show="false">
        <el-select
          v-model="queryParams.factoryId"
          placeholder="请选择所属工厂"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in factoryListOptions"
            :key="dict.deptId"
            :label="dict.name"
            :value="dict.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="所属工厂" prop="factoryId">
        <el-input
          v-model="queryParams.factoryId"
          placeholder="请输入所属工厂"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material_sum:material_sum:add']"
          >生成</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd2"
          v-hasPermi="['material_sum:material_sum:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material_sum:material_sum:edit']"
          >修改</el-button
        >
      </el-col>
       <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material_sum:material_sum:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material_sum:material_sum:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="material_sumList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
    <!--  <el-table-column label="序号" align="center" prop="id" />-->
      <el-table-column label="统计时间" align="center" prop="tjsj" width="180"/>
      <el-table-column label="类别" align="center" prop="type1" />
      <!-- <el-table-column label="产品" align="center" prop="gg" /> -->
      <el-table-column label="物料名称" align="center" prop="ggname" />
<!-- <el-table-column label="物料名称" align="center" prop="ggname" :formatter="laboratory_Biology_sj_Format"/> -->

      <el-table-column label="数量" align="center" prop="counts" />


      <el-table-column label="所属工厂" align="center" prop="factoryName" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
          >导出</el-button            >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material_sum:material_sum:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material_sum:material_sum:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改七率原物料对话框 -->
    <el-dialog fullscreen
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别" prop="type1">
              <!-- <el-input
                v-model="form.type1"
                placeholder="请输入类别(内控、统计月)"
                :disabled="openDisabled"
              /> -->
              <el-select v-model="form.type1" placeholder="请选择类别" :disabled="openDisabled">
                <el-option value="内控指标">内控指标</el-option>
                <el-option value="统计月">统计月</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品" prop="gg">
              <!--  <el-input
                v-model="form.gg"
                placeholder="请输入产品"
                :disabled="openDisabled"
              /> -->
              <!-- <Dialog
                @dialogChange="dialogChange($event)"
                :echoValue="form.ggname"
                parameter="rows"
                value="id"
                label="name"
                :multiple="false"
                search="name/code"
                url="/basic/productInfo/listSelect"
                :column="['产成品编号/code', '产成品名称/name']"
                placeholder="请选择内容"
                :openDisabled="openDisabled"
              /> -->
            <!-- <el-table-column label="检查内容" prop="jcnrId" width="650px">
              <template slot-scope="scope"> -->
                <el-select
                  v-model="form.ggname"
                  placeholder="请选择检查内容"
                  style="width: 100%"
                  :disabled="openDisabled"
                >
                <el-option
                  v-for="dict in Productionline_Hygiene"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictLabel"
                ></el-option>
                </el-select>
              <!-- </template>
            </el-table-column> -->
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="产品" prop="ggname">
              <el-input
                v-model="form.ggname"
                placeholder="请输入产品"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量" prop="counts">
              <el-input
                v-model="form.counts"
                placeholder="请输入数量"
                :disabled="openDisabled"
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="统计时间" prop="tjsj">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.tjsj"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择统计时间"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属工厂" prop="factoryId">
              <!-- <el-input
                v-model="form.factoryId"
                placeholder="请输入所属工厂"
                :disabled="openDisabled"
              /> -->
              <el-select
                v-model="form.factoryId"
                placeholder="请选择所属工厂"
                clearable
                size="small"
                :disabled="true"
              >
                <el-option
                  v-for="dict in factoryListOptions"
                  :key="dict.deptId"
                  :label="dict.name"
                  :value="dict.deptId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterial_sum,
  getMaterial_sum,
  delMaterial_sum,
  addMaterial_sum,
  updateMaterial_sum,
  createMaterial_sum,
} from "@/api/report/complain/material_sum";
import { getFactoryList } from "@/api/report/complain/complain";

export default {
  name: "Material_sum",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 七率原物料表格数据
      material_sumList: [],
      // 所属工厂
      factoryListOptions: [],
      Productionline_Hygiene: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        type1: null,
        gg: null,
        ggname: null,
        counts: null,
        tjsj: null,
        factoryId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryId: [{ required: true, message: "所属工厂不能为空" }],
        type1: [{ required: true, message: "类别不能为空" }],
      },
    };
  },
  created() {
    this.queryParams.factoryId=101;
    this.getList();
    this.getFactoryList();
    this.getDicts("seven_material_ggname").then((response) => {
      this.Productionline_Hygiene = response.data;
    });
  },
  methods: {
    dialogChange(event) {
      // console.log(event)
      this.form.gg = event.options.code;
      this.form.ggname = event.label;
    },
    laboratory_Biology_sj_Format(row, column) {
      return this.selectDictLabel(this.Productionline_Hygiene, row.ggname);
    },
    //工厂信息
    getFactoryList() {
      getFactoryList().then((response) => {
        this.factoryListOptions = response.data;
      });
    },
    /** 查询七率原物料列表 */
    getList() {
      this.loading = true;
      listMaterial_sum(this.queryParams).then((response) => {
        this.material_sumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type1: null,
        gg: null,
        ggname: null,
        counts: null,
        tjsj: null,
        factoryId: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd2() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加七率原物料";
      this.form.factoryId=101;
    },

    handleAdd() {
      if (this.queryParams.tjsj == null) {
        this.msgError("请选择统计时间！");
      } else if (this.queryParams.factoryId == null) {
        this.msgError("请选择所属工厂！");
      } else {
        listMaterial_sum(this.queryParams).then((res) => {
          const arr = new Array();
          res.rows.forEach((item) => {
            if (item.tjsj == this.queryParams.tjsj) {
              arr.push(item.id);
            }
          });
          if (arr.length > 0) {
            this.$confirm("已存在当月记录是否覆盖该记录, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                // 先删除  后添加
                delMaterial_sum(arr);
                this.createMaterial_sum();
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消",
                });
              });
          } else {
            this.createMaterial_sum();
          }
        });
      }
      this.form.factoryId=101;
    },
    createMaterial_sum() {
      this.reset();
      createMaterial_sum(this.queryParams).then((res) => {


        if (res == 0) {
          this.msgError("没有数据！");
        } else {
          this.msgSuccess("新增成功");
        this.resetQuery();
        this.getList();
        }

      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getMaterial_sum(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改七率原物料";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getMaterial_sum(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看七率原物料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMaterial_sum(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addMaterial_sum(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除七率原物料编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMaterial_sum(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    excelhandleView(row){
      this.download(
        "productionProcess/material_sum/exportexcel?id="+row.id,
        {
          ...this.queryParams,
        },
        "七率原物料" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "material_sum/material_sum/export",
        {
          ...this.queryParams,
        },
        "七率原物料" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
